import editForm from "../form.vue";
import { addDialog } from "@/components/ReDialog";
import { message } from "@/utils/message";
import { deviceDetection } from "@pureadmin/utils";
import type { FormItemProps } from "../utils/types";
import { h, ref } from "vue";
import { enterpriseApplyApi } from "@/api/loginApi";
export function useRole() {
  const formRef = ref();
  function openDialog() {
    let formInline = {};
    formInline = {
      name: "",
      socialCode: "",
      shortName: "",
      organizationCode: "",
      dunsNumber: "",
      businessType: "",
      taxIdentifyNumber: "",
      registerPlace: "",
      registerAddress: "",
      legalPerson: "",
      principal: "",
      phone: "",
      registeredCapital: 0,
      foundDate: "",
      businessTerm: "",
      businessLongTerm: "",
      businessScope: ""
    };
    addDialog({
      title: `企业申请`,
      props: {
        formInline: formInline
      },
      width: "55%",
      draggable: true,
      fullscreen: deviceDetection(),
      fullscreenIcon: true,
      closeOnClickModal: false,
      contentRenderer: () => h(editForm, { ref: formRef }),
      beforeSure: (done, { options }) => {
        const FormRef = formRef.value.getRef();
        const curData = options.props.formInline as FormItemProps;
        function chores() {
          message(`企业申请成功!等待管理员审核`, {
            type: "success"
          });
          done();
        }

        FormRef.validate(valid => {
          if (valid) {
            // 表单规则校验通过
            enterpriseApplyApi(curData)
              .then(() => {
                chores();
              })
              .catch(error => {
                message(error.response.data.msg ?? "操作失败,请联系管理员", {
                  type: "error"
                });
              });
          }
        });
      }
    });
  }

  return {
    openDialog
  };
}
